package LeetCode;

public class Code33_search {

    public int search(int[]nums, int target){
        if (nums == null || nums.length == 0){
            return -1;
        }
        int left = 0;
        int right = nums.length - 1;
        while (left <= right){
            int pivot = left + ((right - left) >> 2);
            if (nums[pivot] == target){
                return pivot;
            }
            if (nums[left] <= nums[pivot]){ // 从left到pivot是递增的
                if (nums[left] <= target && target < nums[pivot]){ // target位于(left,pivot)
                    right = pivot - 1;
                } else { // target在pivot右边
                    left = pivot + 1;
                }
            // pivot位于右半递增区
            } else if (nums[pivot] < target && target <= nums[right]){
                left = pivot + 1;
            } else {
                right = pivot - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Code33_search code33_search = new Code33_search();
        System.out.println(code33_search.search(new int[]{4,5,6,7,0,1,2}, 0));
        System.out.println(code33_search.search(new int[]{4,5,6,7,0,1,2}, 3));
    }
}
